<?php
/**
 * @企业类类
 * 1、企业list
 * 2、注册
 * 3、忘记密码
 * 4、修改资料 
 * @作者 
 * 万全强（php）
 */
namespace app\controller;

use app\controller\HomeBase;
use think\facade\Db;

use app\model\Chain;
use app\model\CompanyModel;
use app\model\MonitorModel;

class Company extends HomeBase
{
	private $CompanyModel;
	private $ChainModel;

    public function __construct(){
               
        parent::__construct(); 
        $this->CompanyModel=new CompanyModel();
        $this->ChainModel=new Chain();

    }
    public function GetList(){
    	$page=request()->param('page', 1, 'intval');
    	
    	$limit = request()->param('limit', 10, 'intval');
    	if($limit>15&&$limit!=2000){
    		$limit=15;
    	}
    	$condition=[];
    	if($this->user_id){
    		$condition['user_id']=$this->user_id;
    	}
    	
    	$keywords = request()->param('keywords', '', 'trim');
    	$keywords=preg_replace("/\\d+/",'', trim($keywords));
    	if($keywords){
    		$condition['keywords']=$keywords;
    	}
    	
    	//地方
    	$chain_id = request()->param('chain_id', 0, 'intval');
    	if (!empty($chain_id)) {
    		$condition['chain_id']=$chain_id;
    	}
    	//云端
    	$yun_chain_id = request()->param('yun_chain_id', 0, 'intval');
    	if (!empty($yun_chain_id)) {
    		$condition['yun_chain_id']=$yun_chain_id;
    	}
    		
    	
    	$city_id = request()->param('city_id', 0, 'intval');
    	if (!empty($city_id)) {
    		$condition['city_id']=$city_id;    
    	}
    	
    	$industry_id = request()->param('industry_id', 0, 'intval');
        if (!empty($industry_id)) {
        	$condition['industry_id']=$industry_id;  
        }
        
        $label_id = request()->param('label_id', 0, 'intval');
        if (!empty($label_id)) {
        	$condition['label_id']=$label_id;  
        }
       
    	$field=[
    			'identification',
    			'company_name',
    			'register_money',
    			'legal',
    			'logo',
    			'chain_name',
    			'chain_id',
    			'lat',
    			'lng',
    			'industry_name',
    			'register_time_str',
    			'elephone',
    			'score',			
                'park_name',
    			'contacts'
    	];
    
    	
    	$list=$this->CompanyModel->GetList($field,$condition,$page,$limit,['score'=>'desc']);
    	
    	foreach ($list as &$val){
    		if(isset($val['contacts'])&&isset($val['contacts'][0])){
    			$val['elephone']=$val['contacts'][0]['tel'];
    			$val['legal']=$val['contacts'][0]['contact'];
    		}
    	}
    	return json(['status'=>1,'msg'=>'获取成功','data'=>$list]);
    }
    public  function CompanyDetail(){
    	$identification=request()->param('identification', '', 'trim');
    	if(empty($identification)){
    		return json(['status'=>0,'msg'=>'参数错误','data'=>[]]);
    	}
    	$info=$this->CompanyModel->GetDetail($identification);
    	if(!empty($info)){
    		//查看有无监控
    		$MonitorModel=new MonitorModel();
    		//判断是否存在
    		$Monitor=$MonitorModel->GetDetail(['identification'=>$info['identification'],'type'=>1,'user_id'=>$this->user_id]);
    		
    		$info['IsMonitor']=empty($Monitor)?0:1;
    	}
    	return json(['status'=>1,'msg'=>'获取成功','data'=>$info]);   	
    }
    public  function GetCompanyTotal(){
    	
    	$condition=[];
    	if($this->user_id){
    		$condition['user_id']=$this->user_id;
    	}
    	
    	$keywords = request()->param('keywords', '', 'trim');
    	$keywords=preg_replace("/\\d+/",'', trim($keywords));
    	if($keywords){
    		$condition['keywords']=$keywords;
    	}
    	//地方
    	$chain_id = request()->param('chain_id', 0, 'intval');
    	if (!empty($chain_id)) {
    		$condition['chain_id']=$chain_id;
    	}
    	//云端
    	$yun_chain_id = request()->param('yun_chain_id', 0, 'intval');
    	if (!empty($yun_chain_id)) {
    		$condition['yun_chain_id']=$yun_chain_id;
    	}
    	
    	$city_id = request()->param('city_id', 0, 'intval');
    	if (!empty($city_id)) {
    		$condition['city_id']=$city_id;
    	}
    	
    	$industry_id = request()->param('industry_id', 0, 'intval');
    	if (!empty($industry_id)) {
    		$condition['industry_id']=$industry_id;
    	}
    	
    	$label_id = request()->param('label_id', 0, 'intval');
    	if (!empty($label_id)) {
    		$condition['label_id']=$label_id;
    	}
    	
    	$total=$this->CompanyModel->GetTotal($condition);
    	return json(['status'=>1,'msg'=>'获取成功','data'=>$total]);
    }
    

}